<template>
  <div>
    <MessageWinComponent ref="message-ref"/>
    <ToolBarComponent ref="test-content-bar">
      <SelectComboComponent     input-lable="题目类型"  input-name="type"   group-code="BM0801"/>
      <SelectComboComponent     input-lable="难度级别"  input-name="level"   group-code="BM0802"/>
      <SelectComboComponent     input-lable="所属模块"  input-name="moduel"   group-code="BM0804"/>
      <TextAreaInputComponent   input-lable="题干"     input-name="content" :input-width="900"/>
      <div style="padding-bottom:15px">
      </div>
      <Button text="新增选项/答案" :click-function="addSelect"/>
      <Button text="减少选项/答案" :click-function="subSelect"/>
    </ToolBarComponent>
    <div v-for="index in selectNum" :key="index">
      <InputComponent  :input-lable="index.toString()" :ref="'select-'+index.toString()" :input-width="400"/>
      <SelectComboComponent  input-lable="正确答案标记" :ref="'select-flag'+index.toString()" :input-width="150" group-code="BM0803"/>
    </div>
    <Button text="提交" :click-function="submit"/>
  </div>
</template>

<script>
import TextAreaInputComponent from "@/components/input/TextAreaInputComponent";
import ToolBarComponent from "@/components/toolbar/ToolBarComponent";
import Button from "@/components/button/ButtonComponent";
import SelectComboComponent from "@/components/input/SelectComboComponent";
import InputComponent from "@/components/input/InputComponent";
import {postRequest} from "@/axio2";
import MessageWinComponent from "@/components/win/MessageWinComponent";

export default {
  name: "AddTestQuestion",
  data(){
    return{
      selectNum:0
    }
  },
  methods:{
    addSelect(){
      console.log(this.selectNum);
      if(this.selectNum<9){
        this.selectNum=this.selectNum+1;
      }
    },
    subSelect(){
      console.log(this.selectNum);
      if(this.selectNum>0){
        this.selectNum=this.selectNum-1;
      }
    },
    submit(){
      let param = this.$refs["test-content-bar"].getSubmitParams();
      let num = 1;
      let selections = [];
      while (num<=this.selectNum){
        let selection = this.$refs["select-"+num.toString()][0].getInputValue();
        let flag = this.$refs["select-flag"+num.toString()][0].getInputValue();
        selections.push({'selectNode':num, 'selectContent':selection["value"], 'flag':(("01"===param["type"]||"02"===param["type"])?(""===flag["value"]?'0':flag["value"]):'1')});
        num++;
      }
      param["selectionJson"]=JSON.stringify(selections);
      postRequest(
          "testManage/addQuestion.do",
          param,
          this.successCallback,
          this.failCallback,
          this
      );
    },
    successCallback(resp,component){
      this.selectNum = 0;
      component.$refs["message-ref"].setMessage("新增成功");
      component.$refs["message-ref"].showWin();
    },
    failCallback(resp,component){
      component.$refs["message-ref"].setMessage(resp.data.errorMsg);
      component.$refs["message-ref"].showWin();
    }
  },

  components: {
    MessageWinComponent,
    InputComponent, SelectComboComponent, Button, ToolBarComponent, TextAreaInputComponent}
}
</script>

<style scoped>

</style>